package weeny.face;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class LabeledBox extends Box{
	private JPanel content = new JPanel(new GridBagLayout());
	private GridBagConstraints gbc = new GridBagConstraints();
	public LabeledBox(String title){
		this();
		setBorder(new TitledBorder(title));
	}
	public LabeledBox() {
		super(BoxLayout.Y_AXIS);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		setAlignmentX(0);
		add(content);
	}
	public void addLabel(String label){
		if (!label.endsWith(" ")) {
			label += " ";
		}
		JLabel l = new JLabel(label);
		l.setHorizontalAlignment(JLabel.RIGHT);
		gbc.gridwidth = 1;
		GridBagLayout gbl = (GridBagLayout) content.getLayout();
		gbl.setConstraints(l, gbc);
		content.add(l);
	}
	public void addComp(Component component){
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		GridBagLayout gbl = (GridBagLayout) content.getLayout();
		gbl.setConstraints(component, gbc);
		content.add(component);
	}
	public void addComp(String label, Component component){
		this.addLabel(label);
		this.addComp(component);
		this.addComp(Box.createVerticalStrut(3));
	}
}
